package org.metamodelos2.modelo.conexion;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class LecturaParametros {

	public void leerXML1() throws Exception{
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document documento = docBuilder.parse (new File("conexion.xml"));
		
		Node nodoRaiz = documento.getFirstChild();
		NodeList listaNodosHijos = nodoRaiz.getChildNodes();
		for (int i=0; i<listaNodosHijos.getLength(); i++){
			Node unNodoHijo = listaNodosHijos.item(i);
			if(unNodoHijo.getNodeType()==Node.ELEMENT_NODE)
				System.out.println(unNodoHijo.getNodeName()+"-"+unNodoHijo.getTextContent());
		}
		   
	}
	
	public void leerXML2() throws Exception{
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document documento = docBuilder.parse (new File("conexion1.xml"));
		
		Node nodoRaiz = documento.getFirstChild();
		NamedNodeMap atributos=nodoRaiz.getAttributes();
		Node hostAtributo = atributos.getNamedItem( "host" );
		String host = hostAtributo.getNodeValue();
		System.out.println(host);
		
		Node puertoAtributo = atributos.getNamedItem( "puerto" );
		String puerto = puertoAtributo.getNodeValue();
		System.out.println(puerto);
		
		Node bdAtributo = atributos.getNamedItem( "bd" );
		String bd = bdAtributo.getNodeValue();
		System.out.println(bd);
	}
	
	public void leerPropertiesIS() throws Exception{
		String nombrePROP = "conexion.properties";
		Properties props = new Properties();
		props.load(LecturaParametros.class.getResourceAsStream(nombrePROP));
	
		String host=props.getProperty("host");
		String puerto=props.getProperty("puerto");
		String bd=props.getProperty("bd");
		System.out.println(host);
		System.out.println(puerto);
		System.out.println(bd);
	}
	
	public void leerPropertiesResource(){
		String host=ResourceBundle.getBundle("conexion").getString("host");
		String puerto=ResourceBundle.getBundle("conexion").getString("puerto");
		String bd=ResourceBundle.getBundle("conexion").getString("bd");
		System.out.println(host);
		System.out.println(puerto);
		System.out.println(bd);
	}
	
	public static void main(String[] args) {
		LecturaParametros lectura=new LecturaParametros();
		try {
			Long t1=Calendar.getInstance().getTimeInMillis();
			lectura.leerXML1();
			Long t2=Calendar.getInstance().getTimeInMillis();
			lectura.leerXML2();
			Long t3=Calendar.getInstance().getTimeInMillis();
			lectura.leerPropertiesIS();
			Long t4=Calendar.getInstance().getTimeInMillis();
			lectura.leerPropertiesResource();
			Long t5=Calendar.getInstance().getTimeInMillis();
			System.out.println("xml1: "+(t2-t1));
			System.out.println("xml2: "+(t3-t2));
			System.out.println("prop1: "+(t4-t3));
			System.out.println("prop2: "+(t5-t4));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
